package com.qqtech.qquan.activity.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.activity.dao.ActivityBaseDao;
import com.qqtech.qquan.activity.dao.ActivitySignDao;
import com.qqtech.qquan.activity.enums.ActivityBaseStatusDictEnum;
import com.qqtech.qquan.activity.enums.ActivitySignStatusDictEnum;
import com.qqtech.qquan.activity.model.ActivityBase;
import com.qqtech.qquan.activity.model.ActivityBaseQuery;
import com.qqtech.qquan.activity.model.ActivityBaseVo;
import com.qqtech.qquan.activity.model.ActivitySignQuery;
import com.qqtech.qquan.activity.model.ActivitySignVo;

@Service
public class ActivityBaseServiceImpl extends BaseServiceImpl<ActivityBase> implements ActivityBaseService {
	public static final Logger log = LoggerFactory.getLogger(ActivityBaseServiceImpl.class);

	@Resource
	private ActivityBaseDao activityBaseDao;

	@Resource
	private ActivitySignDao activitySignDao;

	@Override
	protected BaseDao<ActivityBase> getBaseDao() {
		return activityBaseDao;
	}

	@Override
	public ActivityBaseVo handleDetail(Integer id) {
		activityBaseDao.updatePv(id);
		ActivityBaseVo activityBaseVo = activityBaseDao.getById(id);
		return activityBaseVo;
	}

	@Override
	public int show(int id) {
		ActivityBase activity = new ActivityBase();
		activity.setId(id);
		activity.setIsShow(YesNoEnum.YES.getCode());
		return activityBaseDao.updateById(activity);
	}

	@Override
	public int hide(int id) {
		ActivityBase activity = new ActivityBase();
		activity.setId(id);
		activity.setIsShow(YesNoEnum.NO.getCode());
		return activityBaseDao.updateById(activity);
	}

	@Override
	public boolean isActivityCreator(Integer userId, Integer activityId) {
		ActivityBase activity = new ActivityBase();
		activity.setCreatorId(userId);
		activity.setId(activityId);
		return activityBaseDao.queryCount(activity) > 0;
	}

	@Override
	public void autoStatus() {
		ActivityBaseQuery query = new ActivityBaseQuery();
		List<Integer> includeStatus = new ArrayList<Integer>();
		includeStatus.add(ActivityBaseStatusDictEnum.SIGN.getCode());
		includeStatus.add(ActivityBaseStatusDictEnum.PROCEED.getCode());
		query.setIsShow(YesNoEnum.YES.getCode());
		query.setIncludeStatus(includeStatus);
		List<ActivityBaseVo> list = activityBaseDao.queryList(query);
		List<ActivityBase> updateList = new ArrayList<ActivityBase>();
		for (ActivityBaseVo activityBaseVo : list) {
			if (ActivityBaseStatusDictEnum.SIGN.getCode() == activityBaseVo.getStatus()) {// 报名中的活动
				if (TimeUtil.now().after(activityBaseVo.getSignEndAt())) {
					activityBaseVo.setStatus(ActivityBaseStatusDictEnum.PROCEED.getCode());
					updateList.add(activityBaseVo);
				}
			} else if (ActivityBaseStatusDictEnum.PROCEED.getCode() == activityBaseVo.getStatus()) {// 进行中的活动
				activityBaseVo.setStatus(ActivityBaseStatusDictEnum.FINISH.getCode());
				ActivitySignQuery querySign = new ActivitySignQuery();
				querySign.setActivityId(activityBaseVo.getId());
				querySign.setStatus(ActivitySignStatusDictEnum.PROCEED.getCode());
				List<ActivitySignVo> listSign = activitySignDao.queryList(querySign);
				if (null != listSign && listSign.size() > 0) {
					for (ActivitySignVo activitySignVo : listSign) {
						activitySignVo.setStatus(ActivitySignStatusDictEnum.COMPLETE.getCode());
						activitySignDao.updateById(activitySignVo);
					}
				}
				updateList.add(activityBaseVo);
			}
		}
		if (updateList.size() > 0) {
			activityBaseDao.updateInBatch(updateList);
		}
	}

	@Override
	public int setRecommend(Integer id, Integer recommendOidx) {
		ActivityBase updateBase = new ActivityBase();
		updateBase.setId(id);
		updateBase.setIsRecommend(YesNoEnum.YES.getCode());
		updateBase.setRecommendAt(TimeUtil.now());
		updateBase.setRecommendOidx(recommendOidx);
		return activityBaseDao.updateById(updateBase);
	}

	@Override
	public int setUnRecommend(Integer id) {
		ActivityBase updateBase = new ActivityBase();
		updateBase.setId(id);
		updateBase.setIsRecommend(YesNoEnum.NO.getCode());
		updateBase.setRecommendAt(TimeUtil.now());
		updateBase.setRecommendOidx(99);
		return activityBaseDao.updateById(updateBase);
	}

	@Override
	public List<ActivityBaseVo> queryListByCircle(ActivityBaseQuery query, Pageable pageable) {
		return activityBaseDao.queryListByCircle(query, pageable);
	}

	@Override
	public Page<ActivityBaseVo> queryPageByCircle(ActivityBaseQuery query, Pageable pageable) {
		return activityBaseDao.queryPageByCircle(query, pageable);
	}

	@Override
	public List<ActivityBaseVo> queryActivityByUserId(Integer userId) {
		return activityBaseDao.queryActivityByUserId(userId);
	}
}